package com.zhou.mongo.controller;

import com.zhou.mongo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestStudent {

    @Autowired
    MongoTemplate mongoTemplate;

    @GetMapping("/testsave")
    public void save() {
        Student user=new Student();
        user.setId(1);
        user.setName("小明");
        user.setAge(20);
        user.setSex("男");
        user.setLevel("六班");
        Student user2=new Student();
        user2.setId(2);
        user2.setName("小静");
        user2.setAge(18);
        user2.setSex("女");
        user2.setLevel("5班");
        mongoTemplate.save(user,"col");
        mongoTemplate.save(user2,"col");
        System.out.println("mongoDB插入数据成功,集合为col,文档为："+mongoTemplate.getCollection("col"));
        List<Student> list = mongoTemplate.findAll(Student.class,"col");
        System.out.println("mongoDB查询数据成功,集合为col,文档为：");
        for (Student shopDO:list){
            System.out.println(shopDO.getId()+"/"+shopDO.getName());
        }
    }

}


